package cn.migu.music.hangupsms.common.util;

import java.security.MessageDigest;
import java.util.Date;

import org.apache.commons.lang.RandomStringUtils;

import cn.migu.music.common.utils.DateTimeConvertor;
import cn.migu.music.common.utils.LogUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DidUtils {
    public static String generateSeq(String did) {
        StringBuilder sb = new StringBuilder();
        sb.append(did);
        sb.append(DateTimeConvertor.format(new Date(), "yyyyMMddHHmmss"));
        sb.append(RandomStringUtils.randomNumeric(8));
        return sb.toString();
    }

    @SuppressWarnings("all")
    public static String generateMD5String(String string) {
        StringBuilder result = new StringBuilder();
        try {
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.update(string.getBytes("UTF8"));
            byte s[] = m.digest();
            for (int i = 0; i < s.length; i++) {
                result.append(Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6));
            }
        } catch (Exception e) {
            LogUtils.info(e, log, "加密失败");
            result = null;
        }
        return result == null ? null : result.toString();
    }
}
